/**
 * Student Name: Thong, Wang Kit Wilson (50419704)
 *               Yin, Xing (51590277)
 */
#include "Log.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

void 
LogDebug (const char* msg, char* filename, int linenum)
{
#ifndef _NO_DEBUG_LOG
	time_t tp = time (NULL);
	char* timeStr = ctime (&tp);
	timeStr[strlen(timeStr)-1] = '\0';
#ifdef LOG_OUT
	fprintf (LOG_OUT, "%s [DEBUG] %s:%d: %s\n", timeStr, filename, linenum, msg);
#else
	FILE* fp = fopen ("log.txt", "a");
	fprintf (fp, "%s [DEBUG] %s:%d: %s\n", timeStr, filename, linenum, msg);
	fclose (fp);
#endif
#endif
}

void 
LogInfo (const char* msg, char* filename, int linenum)
{
	time_t tp = time (NULL);
	char* timeStr = ctime (&tp);
	timeStr[strlen(timeStr)-1] = '\0';
#ifdef LOG_OUT
	fprintf (LOG_OUT, "%s [INFO]  %s:%d: %s\n", timeStr, filename, linenum, msg);
#else
	fprintf (stdout, "%s [INFO]  %s:%d: %s\n", timeStr, filename, linenum, msg);
	FILE* fp = fopen ("log.txt", "a");
	fprintf (fp, "%s [INFO]  %s:%d: %s\n", timeStr, filename, linenum, msg);
	fclose (fp);
#endif
}

void 
LogWarn (const char* msg, char* filename, int linenum)
{
	time_t tp = time (NULL);
	char* timeStr = ctime (&tp);
	timeStr[strlen(timeStr)-1] = '\0';
#ifdef LOG_OUT
	fprintf (LOG_OUT, "%s [WARN]  %s:%d: %s\n", timeStr, filename, linenum, msg);
#else
	fprintf (stdout, "%s [WARN]  %s:%d: %s\n", timeStr, filename, linenum, msg);
	FILE* fp = fopen ("log.txt", "a");
	fprintf (fp, "%s [WARN]  %s:%d: %s\n", timeStr, filename, linenum, msg);
	fclose (fp);
#endif
}

void 
LogErr (const char* msg, char* filename, int linenum)
{
	time_t tp = time (NULL);
	char* timeStr = ctime (&tp);
	timeStr[strlen(timeStr)-1] = '\0';
#ifdef LOG_OUT
	fprintf (LOG_OUT, "%s [ERR]   %s:%d: %s\n", timeStr, filename, linenum, msg);
#else
	fprintf (stdout, "%s [ERR]   %s:%d: %s\n", timeStr, filename, linenum, msg);
	FILE* fp = fopen ("log.txt", "a");
	fprintf (fp, "%s [ERR]   %s:%d: %s\n", timeStr, filename, linenum, msg);
	fclose (fp);
#endif
}
